package popcorn.ticket.serviceimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.dao.LogsMapper;
import popcorn.ticket.dao.MovieReviewMapper;
import popcorn.ticket.entity.Logs;
import popcorn.ticket.entity.MovieReview;
import popcorn.ticket.service.MovieReviewService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

@Service
public class MovieReviewServiceImpl implements MovieReviewService {

	@Autowired
	private MovieReviewMapper mrm;

	@Autowired
	private LogsMapper lm;

	/**
	 * 后台追评 时间区间查询
	 */
	@Override
	public PageData<MovieReview> getMovieReviewByLike(String keywords, int page, int limit, String username,
			String moviename, String startTime, String endTime,int userid) {
		PageHelper.startPage(page, limit);
		List<MovieReview> list = mrm.getMovieReviewByLike(keywords, username, moviename, startTime, userid, endTime);
		PageInfo<MovieReview> pageInfo = new PageInfo<>(list);
		PageData<MovieReview> pd = new PageData<>(list, page, (int) pageInfo.getTotal(), limit);
		pd.setCode(0);
		pd.setMsg("");
		return pd;
	}


	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public AjaxResponse updateMRSta(List idList, Logs logs) {
		AjaxResponse ar = new AjaxResponse();

		try {
			int n = mrm.UpdateMRSta(idList);
			int n1 = lm.insert(logs);
			ar.setCode(200);
			System.out.println("n" + n);
			System.out.println("n1" + n1);
			System.out.println(1 / n);
			System.out.println(1 / n1);
			ar.setMsg("禁用成功!");
			return ar;
		} catch (Exception e) {
			//e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			ar.setCode(0);
			ar.setMsg("禁用失败!");
			return ar;
		}

	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public AjaxResponse updateMRStaByUser(int reviewid, Logs logs) {

		AjaxResponse ar = new AjaxResponse();
		MovieReview mr = new MovieReview();
		mr.setReviewid(reviewid);
		mr.setReviewstatus(0);
		try {

			int n = mrm.updateByPrimaryKeySelective(mr);
			int n1 = lm.insert(logs);
			System.out.println(1/n);
			System.out.println(1/n1);
			ar.setCode(200);
			ar.setMsg("删除成功");
			return ar;
		} catch (Exception e) {
			//e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			ar.setCode(0);
			ar.setMsg("删除失败!");
			return ar;
		}
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public AjaxResponse addReviewByUser(MovieReview mr) {
		AjaxResponse ar = new AjaxResponse("发布失败", 0);
		System.out.println(mr.getReviewcontent());
		System.out.println(mr.getComment().getCommentid());
		System.out.println(mr.getReviewtime());
		System.out.println(mr.getReviewstatus());
		mr.setReviewid(null);
		
		try {

			int n = mrm.insert(mr);
			System.out.println(n);
			System.out.println(1/n);
			ar.setCode(200);
			ar.setMsg("发布成功");
			return ar;
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return ar;
		}
	}
	
	/**
	 * 加载追评信息
	 */
	@Override
	public PageData<MovieReview> getMVByCommentId(int commentid, int page, int limit) {
		PageHelper.startPage(page, limit);
		List<MovieReview> list = mrm.getMVByCommentId(commentid);
		PageInfo<MovieReview> pageInfo = new PageInfo<>(list);
		PageData<MovieReview> pd = new PageData<>(list, page, (int) pageInfo.getTotal(), limit);
		pd.setCode(0);
		pd.setMsg("");
		return pd;
	}

	
}
